//
//  MKBrowseCollectionViewCell.m
//  ManKeCartoon
//
//  Created by 邹功梁 on 15/10/16.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKBrowseCollectionViewCell.h"

@implementation MKBrowseCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _scrollView.userInteractionEnabled = YES;
        _scrollView.panGestureRecognizer.delaysTouchesBegan = YES;
        _scrollView.contentSize = CGSizeZero;

        _scrollView.delegate = self;
        _scrollView.maximumZoomScale = 2;
        _scrollView.minimumZoomScale = 1;
        
        _tap_Image = [[UITapGestureRecognizer alloc] init];
        [_tap_Image setNumberOfTapsRequired:1];
        _tap_Image.delegate = self;
        [_scrollView addGestureRecognizer:_tap_Image];
        
        _photoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        
        [self.contentView addSubview:_scrollView];
        [_scrollView addSubview:_photoImageView];
    }
    return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if(touch.view != _scrollView){
        return NO;
    }else
        return YES;
}
- (void)setImageInfosModel:(MKImageInfosModel *)model
{
//    [self.imageView setImageUrl:model];
    _scrollView.maximumZoomScale = 2;
    _scrollView.minimumZoomScale = 1;
    _scrollView.contentSize = CGSizeZero;
    CGFloat width = model.width.floatValue;
    CGFloat height = model.height.floatValue;
    [_photoImageView setFrame:CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH * height/ width) / 2, SCREEN_WIDTH, SCREEN_WIDTH * height/ width)];
    
    for (id view in self.contentView.subviews) {
        if ([view isKindOfClass:[PGView class]]) {
            [view removeFromSuperview];
        }
    }
    
    _progressView = [[PGView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _progressView.backgroundColor = [UIColor clearColor];
    _progressView.center = self.contentView.center;
    [self.contentView insertSubview:_progressView atIndex:1];
    
    [_photoImageView sd_setImageWithURL:[NSURL URLWithString:model.current_img_url] placeholderImage:[UIImage imageNamed:@""] options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
//        _progressView.hidden = NO;
        if (expectedSize < 0) {
            expectedSize = 1;
        }
        CGFloat progress = receivedSize /((CGFloat)expectedSize);
        
        _progressView.progress = progress;
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        if(image!=nil && _progressView.progress <1.0f) {
            _progressView.progress = 1.0f;
            [_progressView removeFromSuperview];
        }
        
    }];
    // 设置ScrollView滚动范围 与图片尺寸一致
    _scrollView.contentSize = _photoImageView.frame.size;

}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return _photoImageView;
}

@end
